﻿using System.IO;
using System.Windows.Media.Imaging;

namespace BatchPrint.Extensions;

public static class ImageExtensions
{
    public static BitmapImage ConvertToBitmapImage(this DImage image)
    {
        using var stream = new MemoryStream();
        image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        var bitmap = new BitmapImage();
        bitmap.BeginInit();
        bitmap.CacheOption = BitmapCacheOption.OnLoad;
        bitmap.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
        bitmap.StreamSource = stream;
        bitmap.EndInit();
        bitmap.Freeze(); // 提高性能

        return bitmap;
    }
}
